package com.hitqz.robot.biz.schedule;

import com.hitqz.robot.api.common.entity.BatteryDataInfo;
import com.hitqz.robot.biz.service.BatteryDataInfoService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.hitqz.robot.netty.service.Send2ServerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author xupkun
 * @date 2024/11/15
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class StatisticalSchedule {

    private final RobotDriverService robotDriverService;

    private final BatteryDataInfoService batteryDataInfoService;

    private final Send2ServerService send2ServerService;
    private final RobotService robotService;

    /**
     * 电池数据统计
     */
    @Scheduled(cron = "0/10 * * * * ?")
    public void staticBattery() {
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        if (iRobotPosDto != null) {
            BatteryDataInfo info = new BatteryDataInfo();
            if (iRobotPosDto.getCurrent() != null) {
                info.setBattery(String.format("%.2f", iRobotPosDto.getBatteryLevel()));
                info.setVoltage(String.format("%.2f", iRobotPosDto.getVoltage()));
                info.setCurrent(String.format("%.2f", Math.abs(Double.parseDouble(iRobotPosDto.getCurrent()))));
                batteryDataInfoService.save(info);
                send2ServerService.uploadBatteryLevel(robotService.getDefaultRobot().getRobotSn(), info);
            }
        } else {
            log.info("数据为空");
        }
    }

}
